package bll

import (
	"context"
	log "manager/collector/logger"

	"manager/event"
	"manager/model"
	"manager/model/entity"
	"manager/store"
	"manager/store/postgres"
	"time"
)

type control struct {
	iControl store.IControl
}

var Control = &control{
	iControl: postgres.Control,
}

func (a *control) init() func() {
	return func() {}
}

func (a *control) onEvent(data *event.Data) {
	if data == nil || data.Type != event.Event_Control_Report {
		return
	}

	// 解析参数
	obj := data.Data.(*event.ControlReport)
	if obj == nil {
		log.Error("control onEvent err")
		return
	}

	// 查询中控信息
	info, err := postgres.Control.Find(context.Background(), &model.ControlInfoRequest{
		SnCode: &obj.SnCode,
	})
	if err != nil {
		return
	}

	// 更新中控信息
	if info.Id > 0 && obj.Data != nil {
		err = postgres.Control.Update(context.Background(), info.Id, map[string]interface{}{
			"mac":   obj.Data.CcuMac,
			"imei":  obj.Data.SimImei,
			"iccid": obj.Data.SimIccid,
			"imsi":  obj.Data.SimImsi,
		})
		if err != nil {
			log.Errorf("control onEvent update err:%v", err)
		}
	}
}

// Create 创建
func (a *control) Create(ctx context.Context, in *model.ControlCreateRequest) (id int64, err error) {
	// 构建创建现场数据
	c := buildControl(in)
	id, err = a.iControl.Create(ctx, c)
	return
}

// Update 更新
func (a *control) Update(ctx context.Context, in *model.ControlUpdateRequest) error {
	var (
		dict = make(map[string]interface{})
	)

	updateAt := time.Now().Unix()
	in.UpdatedAt = &updateAt

	if in.FrameCode != nil {
		dict["frame_code"] = in.FrameCode
	}

	if in.FirmwareId != nil {
		dict["firmware_id"] = in.FirmwareId
	}

	if in.FirmwareVersion != nil {
		dict["firmware_version"] = in.FirmwareVersion
	}

	if in.SnCode != nil {
		dict["sn_code"] = in.SnCode
	}

	if in.Mac != nil {
		dict["mac"] = in.Mac
	}

	if in.Imei != nil {
		dict["imei"] = in.Imei
	}

	if in.Iccid != nil {
		dict["iccid"] = in.Iccid
	}

	if in.Imsi != nil {
		dict["imsi"] = in.Imsi
	}

	if in.UpdatedAt != nil {
		dict["updated_at"] = in.UpdatedAt
	}

	// do other update here
	return a.iControl.Update(ctx, in.Id, dict)
}

// Delete 删除
func (a *control) Delete(ctx context.Context, in *model.ControlDeleteRequest) error {
	return a.iControl.Delete(ctx, in.Id)
}

// List 列表查询
func (a *control) List(ctx context.Context, in *model.ControlListRequest) (*model.ControlListResponse, error) {
	var (
		err   error
		total int
		list  []*entity.Control
		out   = &model.ControlListResponse{}
	)

	if total, list, err = a.iControl.List(ctx, in); err != nil {
		return nil, err
	}

	out.Total = total
	out.List = model.ControlsEntityToDto(list)

	return out, nil
}

// Find 列表查询
func (a *control) Find(ctx context.Context, in *model.ControlInfoRequest) (*model.ControlInfo, error) {
	var (
		err  error
		data *entity.Control
		out  = &model.ControlInfo{}
	)

	if data, err = a.iControl.Find(ctx, in); err != nil {
		return nil, err
	}

	out = model.ControlEntityToDto(data)
	return out, nil
}

// buildControl 构建创建数据现场
func buildControl(in *model.ControlCreateRequest) *entity.Control {
	// todo: check the entity is required
	return &entity.Control{

		FrameCode: in.FrameCode,

		FirmwareId: in.FirmwareId,

		FirmwareVersion: in.FirmwareVersion,

		SnCode: in.SnCode,

		Mac: in.Mac,

		Imei: in.Imei,

		Iccid: in.Iccid,

		Imsi: in.Imsi,

		CreatedAt: time.Now().Unix(),

		UpdatedAt: time.Now().Unix(),
	}
}
